﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.Business.Helper;
using StoreTransport.Resources;
using StoreTransport.DAO;
using StoreTransport.Business.Constants;

namespace StoreTransport.Presentation.Forms {
    public partial class LoginForm : Form {
        public LoginForm() {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e) {
            _tbxLoginName.Select();
        }

        private void _btnLogin_Click(object sender, EventArgs e) {
            Login();
        }

        private void Login() {
            ///////// comment this code to run without loging
            //string errorMessage = CommonHelper.CheckRequiredField(_tbxLoginName.Text, _lblLoginName.Text);
            //errorMessage += CommonHelper.CheckRequiredField(_tbxPassWord.Text, _lblPassWord.Text);
            //if (!string.IsNullOrEmpty(errorMessage)) {
            //    MessageBox.Show(errorMessage, StoreTransportResource.ErrorMessage_ErrorMessageTitle);
            //    return;
            //}
            //StoreTransportDataContext context = CommonHelper.GetContext();
            //var users = context.Users.Where(c => c.Username.Equals(_tbxLoginName.Text) &&
            //    c.Password.Equals(_tbxPassWord.Text)).FirstOrDefault();
            //if (users == null) {
            //    MessageBox.Show(StoreTransportResource.ErrorMessage_WrongUserNameOrPassword);
            //    return;
            //}


            MDIMainForm mainForm = (MDIMainForm)this.MdiParent;


            /////////// comment this code to run without loging
            //List<string> roles = users.Role1.Description.Split(new string[] { CommonConstant.ScreenSeparator },
            //        StringSplitOptions.RemoveEmptyEntries).ToList();

            //foreach (ToolStripMenuItem menu in mainForm.MainMenuStrip.Items) {
            //    if (!roles.Contains(menu.Name)) {
            //        bool hasChild = false;
            //        foreach (ToolStripMenuItem submenu in menu.DropDownItems) {
            //            if (roles.Contains(submenu.Name)) {
            //                hasChild = true;
            //            } else {
            //                submenu.Visible = false;
            //            }
            //        }
            //        if (!hasChild) {
            //            menu.Visible = false;
            //        }
            //    }
            //}
            //mainForm.user = users;

            mainForm.MainMenuStrip.Visible = true;
            this.Close();
        }

        private void _tbnSetupDBPath_Click(object sender, EventArgs e) {
            CommonHelper.OpenForm(this, new ConfigurationForm());
        }

        private void _tbxPassWord_KeyUp(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                Login();
            }
        }
    }
}
